﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace 播放器
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button_Start_Click(object sender, EventArgs e)
        {           
            musicPlayer.Ctlcontrols.play();             
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.musicPlayer.Size = new System.Drawing.Size(402, 258);
           
            //musicPlayer.Size.Height = 258;
            //musicPlayer.Size.Width = 402;
           
            listBox_ImportList.SelectionMode = SelectionMode.MultiExtended;
            musicPlayer.settings.autoStart = false;
          
            imageDic.Add("静音",Image.FromFile(@"E:\learn\c#\my_vs_test\MY_VS_TEST\_VS2017FirstTest\VSCTEST\MY_WinformLearn\DO_YOU_LOVE_ME\播放器\res\静音.png"));
            imageDic.Add("外音",Image.FromFile(@"E:\learn\c#\my_vs_test\MY_VS_TEST\_VS2017FirstTest\VSCTEST\MY_WinformLearn\DO_YOU_LOVE_ME\播放器\res\外音.png"));

            label1.Image = imageDic["静音"];         
            label1.Size = label1.Image.Size;
            label1.Tag = "1";

            timer_AutoPlayBackOne.Interval = 1000;
            timer_AutoPlayBackOne.Start();
        }

        private void button_Pause_Click(object sender, EventArgs e)
        {
            musicPlayer.Ctlcontrols.pause();
        }

        private void button_Stop_Click(object sender, EventArgs e)
        {
            musicPlayer.Ctlcontrols.stop();
        }
        List<string> _musicPath = new List<string>();
        private bool pauseFlag = false;
        private void button_PlayorPause_Click(object sender, EventArgs e)
        {
            if (listBox_ImportList.SelectedIndex > -1)
            {
                if (button_PlayorPause.Text == "播放")
                {
                    if (pauseFlag == false)
                    {
                        //pauseFlag = true;
                        musicPlayer.URL = _musicPath[listBox_ImportList.SelectedIndex];
                    }                   
                    musicPlayer.Ctlcontrols.play();                    
                    button_PlayorPause.Text = "暂停";
                }
                else
                {
                    musicPlayer.Ctlcontrols.pause();
                    button_PlayorPause.Text = "播放";
                    pauseFlag = true;
                }
            }
            
        }

        private void listBox_ImportList_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (listBox_ImportList.SelectedIndex != -1)
            {
                musicPlayer.URL = _musicPath[listBox_ImportList.SelectedIndex];
                musicPlayer.Ctlcontrols.play();
                button_PlayorPause.Text = "暂停";
            }
        }

        private void button_PreOne_Click(object sender, EventArgs e)
        {
            try
            {
                int index = listBox_ImportList.SelectedIndex;

                listBox_ImportList.ClearSelected();

                index--;
                if (index < 0)
                {
                    index = listBox_ImportList.Items.Count - 1;
                }
                listBox_ImportList.SelectedIndex = index;

                musicPlayer.URL = _musicPath[index];
                musicPlayer.Ctlcontrols.play();
            }
            catch { }            
        }

        private void button_BackOne_Click(object sender, EventArgs e)
        {
            PlayNextOne();
        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            int temp = listBox_ImportList.SelectedIndices.Count;
            for (int i = 0; i < temp; i++)
			{
                _musicPath.Remove(_musicPath[listBox_ImportList.SelectedIndex]);
                listBox_ImportList.Items.RemoveAt(listBox_ImportList.SelectedIndex);
			}
            
        }

        private void 添加ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.InitialDirectory = @"F:\CloudMusic";
            ofd.Multiselect = true;
            ofd.DefaultExt = ".mp3";
            ofd.Filter = "所有文件|*.*|mp3|*.mp3|视频文件|*.mp4|wav|*.wav";
            ofd.ShowDialog();

            string[] musicList = ofd.FileNames;
            _musicPath.AddRange(musicList);

            for (int i = 0; i < musicList.Length; i++)
            {
                listBox_ImportList.Items.Add(Path.GetFileNameWithoutExtension(musicList[i]));
            }
        }

        Dictionary<string,Image> imageDic = new Dictionary<string,Image>();  
      
        private void label1_Click(object sender, EventArgs e)
        {
            if (label1.Tag.ToString() == "1")
            {
                musicPlayer.settings.mute = true;
                label1.Image = imageDic["外音"];
                label1.Tag = "2";
            }
            else
            {
                musicPlayer.settings.mute = false;
                label1.Image = imageDic["静音"];
                label1.Tag = "1";
            }           
        }

        private void PlayNextOne()
        {
            try
            {
                int index = listBox_ImportList.SelectedIndex;

                listBox_ImportList.ClearSelected();

                index++;
                if (index >= listBox_ImportList.Items.Count)
                {
                    index = 0;
                }
                listBox_ImportList.SelectedIndex = index;

                musicPlayer.URL = _musicPath[index];
                musicPlayer.Ctlcontrols.play();
            }
            catch { }
        }
        
        private void button_AddVolume_Click(object sender, EventArgs e)
        {
            musicPlayer.settings.volume += 10;           
        }

        private void button_LowerVolume_Click(object sender, EventArgs e)
        {
            musicPlayer.settings.volume -= 10;
        }

        private void timer_AutoPlayBackOne_Tick(object sender, EventArgs e)
        {
            double diff = 0;
            if (musicPlayer.playState == WMPLib.WMPPlayState.wmppsPlaying)
            {
                label2.Text = musicPlayer.Ctlcontrols.currentItem.duration.ToString()+"\r\n";
                label2.Text += musicPlayer.Ctlcontrols.currentItem.durationString + "\r\n";
                label2.Text += musicPlayer.Ctlcontrols.currentPosition.ToString() + "\r\n";
                label2.Text += musicPlayer.Ctlcontrols.currentPositionString + "\r\n";

                diff = musicPlayer.Ctlcontrols.currentItem.duration - musicPlayer.Ctlcontrols.currentPosition;
                if (diff < 1)
                {
                    PlayNextOne();
                }

                showSongLyric(@"F:\CloudMusic\Lyric\"+Path.GetFileNameWithoutExtension(_musicPath[listBox_ImportList.SelectedIndex])+".lrc");
            }

            
        }

        private int currentLineCount = 0;
        private string[] lyricbuff;
        private void showSongLyric(string SongPath)
        {
            string currentLyricTime = "";
            string currentLyricText = "";

            if (File.Exists(SongPath))
            {
                // 读取歌词文件到缓存区
                if (lyricbuff == null)
                {
                    lyricbuff = File.ReadAllLines(SongPath);
                }
                // 获取当前歌词的显示时间
                currentLyricTime = FormatLyric(lyricbuff[currentLineCount])[0];

                // 跟歌词显示时间显示歌词
                if (currentLyricTime.Replace(".",":") == musicPlayer.Ctlcontrols.currentPositionString)
                {
                    
                    currentLyricText = FormatLyric(lyricbuff[currentLineCount])[1];
                    label_SongLyric.Text = currentLyricText;
                    currentLineCount++;
                    if (currentLineCount >= lyricbuff.Length)
                    {
                        currentLineCount = 0;
                    }
                }

            }
            else
            {
                label_SongLyric.Text = "暂无该歌曲歌词";
            }
        }
        private string[] FormatLyric(string arg)
        {
            string[] currentLine = new string[2];
            currentLine[0] = arg.Substring(1, 5);
            currentLine[1] = arg.Substring(10);
            return currentLine;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //showSongLyric(@"F:\CloudMusic\Lyric\Aaron Sprinkle - Whisper Something.lrc");
            //MessageBox.Show(musicPlayer.Size.Width.ToString() + "  " + musicPlayer.Size.Height.ToString());
        }
        
    }
}
